package cn.stylefeng.guns.modular.fi.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.model.params.BasicCarrierParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCarrierResult;
import cn.stylefeng.guns.modular.basic.service.BasicCarrierService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetails;
import cn.stylefeng.guns.modular.fi.entity.FiFreightDetailsLog;
import cn.stylefeng.guns.modular.fi.model.params.FiFreightDetailsParam;
import cn.stylefeng.guns.modular.fi.model.result.FiFreightDetailsResult;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsLogService;
import cn.stylefeng.guns.modular.fi.service.FiFreightDetailsService;
import cn.stylefeng.guns.sys.modular.system.entity.Role;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.RoleService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


/**
 * 控制器
 *
 * @author zhangzh
 * @Date 2024-11-04 11:08:38
 */
@Controller
@RequestMapping("/fiFreightConfirm")
public class FiFreightConfirmController extends BaseController {

    private String PREFIX = "/rec/carrierRecFeeConfirm";

    @Autowired
    private FiFreightDetailsService fiFreightDetailsService;

    @Autowired
    private FiFreightDetailsLogService fiFreightDetailsLogService;

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private BasicCarrierService basicCarrierService;

    @Autowired
    private BasicOperateService basicOperateService;

    /**
     * 跳转到主页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("")
    public String index(Model model) {
        User user = userService.getcurrentUser();
        Long deptId = user.getDeptId();
        Long deliverId = user.getDeliverId();
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId, deliverId, user.getOperateId()));
        return PREFIX + "/carrierRecFeeConfirm.html";
    }

    /**
     * 新增页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/fiFreightDetails_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/carrierRecFeeConfirm_edit.html";
    }

    /**
     * 新增接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.add(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.update(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FiFreightDetailsParam fiFreightDetailsParam) {
        this.fiFreightDetailsService.delete(fiFreightDetailsParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FiFreightDetailsParam fiFreightDetailsParam) {
        FiFreightDetails detail = this.fiFreightDetailsService.getById(fiFreightDetailsParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author zhangzh
     * @Date 2024-11-04
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FiFreightDetailsParam fiFreightDetailsParam, String condition) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isEmpty(fiFreightDetailsParam.getProcessStatus())){
            fiFreightDetailsParam.setProcessStatus("d");
        }
//        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(fiFreightDetailsParam.getOperateId())){
//            fiFreightDetailsParam.setOperateId(user.getOperateId());
//        }
        //查询 已推送 和已确认 状态数据
//        List<String> processStatusList = new ArrayList<>();
//        processStatusList.add("已推送");
//        processStatusList.add("已确认");
//        fiFreightDetailsParam.setProcessStatusList(processStatusList);
//        fiFreightDetailsParam.setCarrierId(-1L);
        fiFreightDetailsParam.setDeptId(user.getDeptId());
        String roleId = user.getRoleId();
        if (ToolUtil.isNotEmpty(roleId)) {
            for (String rId : roleId.split(",")) {
                Role byId = roleService.getById(rId);
                if (byId != null && byId.getName().contains("承运商")) {
                    fiFreightDetailsParam.setCarrierUserCode(user.getAccount());
                    fiFreightDetailsParam.setDeptId(null);
//                    BasicCarrierParam param = new BasicCarrierParam();
//                    param.setUserId(user.getUserId());
//                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
//                    if (bySpec != null) {
//                        fiFreightDetailsParam.setCarrierId(Long.valueOf(bySpec.getId()));
//                    }
                }
            }
        }

        return this.fiFreightDetailsService.findPageBySpec(fiFreightDetailsParam, condition);
    }


    @ResponseBody
    @RequestMapping("/listAll")
    public List<FiFreightDetailsResult> listAll(FiFreightDetailsParam fiFreightDetailsParam, String condition) {
        User user = userService.getcurrentUser();
        if(ToolUtil.isEmpty(fiFreightDetailsParam.getProcessStatus())){
            fiFreightDetailsParam.setProcessStatus("d");
        }
//        if(ToolUtil.isNotEmpty(user.getOperateId()) && ToolUtil.isEmpty(fiFreightDetailsParam.getOperateId())){
//            fiFreightDetailsParam.setOperateId(user.getOperateId());
//        }
        //查询 已推送 和已确认 状态数据
//        List<String> processStatusList = new ArrayList<>();
//        processStatusList.add("已推送");
//        processStatusList.add("已确认");
//        fiFreightDetailsParam.setProcessStatusList(processStatusList);
//        fiFreightDetailsParam.setCarrierId(-1L);
        fiFreightDetailsParam.setDeptId(user.getDeptId());
        String roleId = user.getRoleId();
        if (ToolUtil.isNotEmpty(roleId)) {
            for (String rId : roleId.split(",")) {
                Role byId = roleService.getById(rId);
                if (byId != null && byId.getName().contains("承运商")) {
                    fiFreightDetailsParam.setCarrierUserCode(user.getAccount());
                    fiFreightDetailsParam.setDeptId(null);
//                    BasicCarrierParam param = new BasicCarrierParam();
//                    param.setUserId(user.getUserId());
//                    BasicCarrierResult bySpec = basicCarrierService.findBySpec(param, user.getOperateId());
//                    if (bySpec != null) {
//                        fiFreightDetailsParam.setCarrierId(Long.valueOf(bySpec.getId()));
//                    }
                }
            }
        }

        return this.fiFreightDetailsService.listAll(fiFreightDetailsParam, condition);
    }

    /**
     * 审核
     *
     * @param ids
     * @return
     */
    @RequestMapping("/shenhe")
    @ResponseBody
    public ResponseData shenhe(@RequestParam("ids") String ids) {
        return this.fiFreightDetailsService.shenhe(ids);
    }

    @RequestMapping("/queren")
    @ResponseBody
    public ResponseData queren(@RequestParam("ids") String ids) {
        return this.fiFreightDetailsService.queren(ids);
    }

    @RequestMapping("/shensu")
    @ResponseBody
    public ResponseData shensu(FiFreightDetailsParam fiFreightDetailsParam) {
        return this.fiFreightDetailsService.shensu(fiFreightDetailsParam);
    }


    /**
     * 推送
     *
     * @return
     */
//    @RequestMapping("/tuisong")
//    @ResponseBody
//    public ResponseData tuisong(@RequestParam("ids") String ids) {
//        return this.fiFreightDetailsService.tuisong(ids);
//    }

    // 时序图页面
    @RequestMapping("/tooTimeLine")
    public String tooTimeLine(Long detailsId) {
        return PREFIX + "/tooTimeLine.html";
    }

    @RequestMapping("/tooTimeLineDetailed")
    @ResponseBody
    public ResponseData tooTimeLineWeb(Long detailsId) {
        List<FiFreightDetailsLog> list = fiFreightDetailsLogService.lambdaQuery()
                .eq(FiFreightDetailsLog::getDetailsId, detailsId)
                .orderByAsc(FiFreightDetailsLog::getOpTime)
                .list();
        return ResponseData.success(list);
    }

}


